//
//  GoodsQRCode.m
//  phmall
//
//  Created by admin on 2017/10/18.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "GoodsQRCode.h"
#import <CoreImage/CoreImage.h>
@interface GoodsQRCode ()
@property(nonatomic,strong)UIImageView *QRcode;
@end

@implementation GoodsQRCode

-(instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor whiteColor];
        UIView *head=[[UIView alloc]init];
        [self addSubview:head];
        ViewBorderRadius(head, 0, 1, [UIColor groupTableViewBackgroundColor]);
        head.backgroundColor=[UIColor whiteColor];
        [head mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self);
            make.height.offset(44);
        }];

        UILabel *class=[[UILabel alloc]init];
        [self addSubview:class];
        class.text=Localized(@"二维码");
        class.textColor=[UIColor colorWithHexString:@"#181818"];
        class.font=[UIFont systemFontOfSize:17];
        [class mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(head).offset(20);
            make.centerY.equalTo(head);
        }];

        self.QRcode=[[UIImageView alloc]init];
        ViewBorderRadius(self.QRcode, 0, 1, [UIColor blackColor]);
        [self addSubview:self.QRcode];
        self.QRcode.image=[UIImage imageNamed:@"图片"];
        [self.QRcode mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(head.mas_bottom).offset(10);
            make.width.height.offset(200);
//            make.bottom.equalTo(self).offset(-10);
        }];
    }
    return self;
}
/**
 *  生成二维码
 */
- (void)creatCIQRCodeImage:(NSString *)uid
{
    // 1.创建过滤器，这里的@"CIQRCodeGenerator"是固定的
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复默认设置
    [filter setDefaults];
    
    // 3. 给过滤器添加数据
    NSData *data = [uid dataUsingEncoding:NSUTF8StringEncoding];
    // 注意，这里的value必须是NSData类型
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 4. 生成二维码
    CIImage *outputImage = [filter outputImage];
    
    // 5. 显示二维码
//    UIImage *image=[[UIImage imageWithCIImage:outputImage];
   
    self.QRcode.image = [UIImage creatNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
}
@end
